﻿using System.Collections.Generic;
using System.IO;
using LZX.MEditor.Enum;
using LZX.MEditor.LZXStatic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace LZX.MEditor.Window.Item
{
    public class RunTimeItemBase
    {
        public VisualTreeAsset uxml;
        public StyleSheet uss;
        public VisualElement root;
        
        public Label Name;
        public Label Size;
        public DropdownField SizeDropDown;
        public Label Count;
        public Label Group;
        public Button btn_ex;
        public VisualElement extend_view;
        public VisualElement root_main;
        protected VisualElement SizeContainer;
        protected ScrollView DependenciesContainer;
        private bool isExtended;

        protected VisualElement BG;
        public RunTimeItemBase()
        {
            uxml = LZXEditorResources.GetRunTimeItemUXML();
            uss = LZXEditorResources.GetRunTimeItemUSS();
            root = uxml.CloneTree();
            root.styleSheets.Add(uss);
            
            btn_ex = root.Q<Button>("btn_extend");
            btn_ex.clicked += OnExtendClick;
            Name = root.Q<Label>("label_name");
            Size = root.Q<Label>("label_loadstatu");
            SizeDropDown = root.Q<DropdownField>("dropdown_loadstatu");
            SizeDropDown.RegisterValueChangedCallback(OnSizeChange);
            Count = root.Q<Label>("label_size");
            Group = root.Q<Label>("label_count");
            extend_view = root.Q<VisualElement>("dependenciescontainer");
            SizeContainer = root.Q<VisualElement>("dropdownlabelcontainer");
            DependenciesContainer = root.Q<ScrollView>("dependenciescontainer");
            DependenciesContainer.style.display = DisplayStyle.None;
            root_main = root.Q<VisualElement>("rootmain");
            
            BG = root.Q<VisualElement>("BG");
        }
        private void OnExtendClick()
        {
            isExtended = !isExtended;
            DependenciesContainer.style.display = isExtended? DisplayStyle.Flex : DisplayStyle.None;
        }
        private void OnSizeChange(ChangeEvent<string> evt)
        {
            switch (SizeDropDown.index)
            {
                case 0:
                    Size.text = LoadStatuHelper.LoadedAssets[Name.text].size.ToString("F2"); 
                    break;
                case 1:
                    Size.text = (LoadStatuHelper.LoadedAssets[Name.text].size/8).ToString("F2"); 
                    break;
                case 2:
                    Size.text = (LoadStatuHelper.LoadedAssets[Name.text].size/1024).ToString("F2"); 
                    break;
                case 3:
                    Size.text = (LoadStatuHelper.LoadedAssets[Name.text].size/(1024*1024)).ToString("F2"); 
                    break;
            }
        }
        public void SetLoadBG(bool IsLoaded)
        {
            root_main.style.backgroundColor = IsLoaded? Color.yellow : Color.gray;
        }
    }
}